
package com.way.filemanager.util;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.way.filemanager.R;

import hwdroid.dialog.AlertDialog;
import hwdroid.dialog.Dialog;
import hwdroid.dialog.DialogInterface;

import java.io.File;

public class FileRenameDialog {
    Activity mActivity;
    FileRenameDialog_OnResultCb mOnResultCb;
    Dialog mDialog;
    EditText mEditText;

    public FileRenameDialog(Activity m, int titleResId, FileInfo filename) {
        mActivity = m;
        final FileInfo file = filename;
        LayoutInflater factory = LayoutInflater.from(mActivity);
        final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry_mkdir, null);
        mEditText = (EditText) textEntryView.findViewById(R.id.mkdir_edit);
        mEditText.setText(filename.GetFileName());

        mDialog = new AlertDialog.Builder(mActivity)
                .setTitle(titleResId)
                .setView(textEntryView)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        int ret = 0;
                        String newname = mEditText.getText().toString();
                        String newFilename= null;
                        newname = newname.trim();
                        if (newname.equalsIgnoreCase("")) {
                            ret = -1;
                        } else {
                            newFilename = file.GetFilePath() + File.separator
                                    + mEditText.getText().toString();
                            File mFile = new File(file.GetFullPath());
                            File mRenameTo = new File(newFilename);
                            if(mRenameTo.exists())
                            {
                                DialogHelper.showToast(mActivity, R.string.RenameFileExist,
                                        Toast.LENGTH_SHORT);
                                ret = -2;
                            }
                            else
                            {
                                if (mFile.renameTo(mRenameTo)) {
                                    ret = 0;
                                }
                                //Bugid:103592
                                CommonFunc.StartDeleteMediaFile();
                                CommonFunc.DeleteMediaFile(mRenameTo);
                                CommonFunc.EndDeleteMediaFile(mActivity,false);
                            }
                        }
                        if (ret == -1) {
                            DialogHelper.showToast(mActivity, R.string.RenameFail,
                                    Toast.LENGTH_SHORT);
                        }
                        if (mOnResultCb != null && newFilename != null)
                            mOnResultCb.Resultcb(ret, newFilename);
                        /* User clicked OK so do some stuff */
                    }
                }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked cancel so do some stuff */
                    }
                }).create();
    }

    public void show(FileRenameDialog_OnResultCb cb) {
        mOnResultCb = cb;
        mDialog.show();
        //DialogUtils.fromBottomToTop(mDialog);
        //bug109831
        mEditText.requestFocus();
        mEditText.setSelection(0);
        //DialogUtils.NormalCenter(mDialog);

    }

    public interface FileRenameDialog_OnResultCb {
        /**
         * @param Res :Result code
         * @param r :fullpath after renamed
         */
        int Resultcb(int Res, String r);
    }

}
